#!/usr/bin/env python
#-*-coding:utf-8-*-
# ---------------------------------
# version:  1.0
#
# create-time:      <2008/12/03 01:02:35>
# last-update-time: <halida 12/20/2008 12:33:34>
# ---------------------------------
# module : main

from qtlib import *
import entity
import view,list,filt,reminder,about

class Main(QMainWindow):
    def __init__(self,*args):
        QMainWindow.__init__(self,*args)
        self.pool = entity.EntityPool()
        #component
        self.entityFilter = filt.EntityFilter(self.pool)
        self.entityList = list.EntityList(self.pool)
        self.entityView = view.EntityView(self.pool)
        self.reminder = reminder.Reminder(self.pool)
        self.setMenu()
        #layout
        self.dock(self.entityFilter,Qt.LeftDockWidgetArea,"filter")
        self.setCentralWidget(self.entityList)
        self.dock(self.entityView,Qt.RightDockWidgetArea,"viewer")
        #event
        #filter -> lister
        conn(self.entityFilter,"selected",self.entityList.changeTags)
        #lister -> viewer
        conn(self.entityList,"selected",self.entityView.getEntity)
        #viewer -> lister
        conn(self.entityView,view.ENTITY_CHANGED,self.entityList.refresh)
        #pool -> lister
        #conn(self.pool,entity.POOL_CHANGED,self.entityList.refresh)
        #process
        self.entityFilter.refresh()
        self.reminder.run()

    def setMenu(self):
        menubar = QMenuBar(self)
        #action
        self.saveAction = QAction("&Save",self)
        self.saveToXMLAction = QAction("save to &Xml",self)
        self.exitAction = QAction("&Exit",self)
        self.aboutAction = QAction("&About",self)
        #file
        filemenu = QMenu("&File",self)
        filemenu.addAction(self.saveAction)
        filemenu.addAction(self.saveToXMLAction)
        filemenu.addAction(self.exitAction)
        #help
        helpmenu = QMenu("&Help",self)
        helpmenu.addAction(self.aboutAction)
        #layout
        menubar.addMenu(filemenu)
        menubar.addMenu(helpmenu)
        self.setMenuBar(menubar)
        #event
        conn(self.aboutAction,"triggered()",self.about)
        conn(self.saveAction,"triggered()",self.pool.save)
        conn(self.exitAction,"triggered()",self.exit)
        conn(self.saveToXMLAction,"triggered()",self.saveToXML)
        conn(app,"lastWindowClosed ()",self.exit)

    def dock(self,widget,side=Qt.LeftDockWidgetArea,name=""):
        dockWidget = QDockWidget(name,self)
        dockWidget.setWidget(widget)
        self.addDockWidget(side,dockWidget)

    def exit(self):
        self.pool.save()
        app.exit()

    def about(self):
        about.about()

    def saveToXML(self):
        #get file name
        file = QFileDialog.getSaveFileName(self)
        file = unicode(file)
        if file == "":return
        #save
        self.pool.saveToXML(file)

def main():
    m = Main()
    m.show()
    app.exec_()

if __name__=="__main__":
    main()
